$(function () {

    layui.use(['layer', 'form', 'element'], function () {
        var layer = layui.layer,
            form = layui.form(),
            element = layui.element();

        var index = 0;

        // 立项时间
        $('.pj_add_pro_time').datetimepicker({
            language: 'zh-CN',
            format: 'yyyy-mm-dd',
            weekStart: 1,
            todayBtn: 1,
            autoclose: 1,
            todayHighlight: 1,
            /*startDate : new Date(),*/
            pickerPosition: 'bottom-right',
            startView: 2,
            minView: 2,
            forceParse: 0
        });

        // 添加一行
        $('.add-row-btn').click(function () {
            var html = [
                '<tr>',
                '<td width="220">',
                '<select class="layui-input" name="details[0][stage]">',
                '<volist name="lifeCycles" id="lifeCycle">',
                '<option value="{$lifeCycle.name}">{$lifeCycle.name}</option>',
                </volist>
                </select>
                </td>
                <td>
                <input type="text" name="details[0][items]" class="layui-input" placeholder="节点">
                </td>
                <td width="150">
                <input type="text" name="details[0][plan_start_time]"
                class="layui-input plan_start_time" readonly
                placeholder="计划开始时间">
                </td>
                <td width="150">
                <input type="text" name="details[0][plan_stop_time]"
                class="layui-input plan_stop_time" readonly
                placeholder="计划结束时间">
                </td>
                <td>
                <button class="remove-row btn btn-danger">
                <i class="icon-trash"></i>
                </button>
                </td>
                </tr>
            ];
        });

        //删除一行
        $('#plan-table').on('click', '.remove-row', function () {
            var _tr = $(this).parents('tr');
            var _prev = _tr.prev();
            _prev.find('.btn-group button').removeAttr('disabled');
            _tr.remove();
            if ($('#plan-table tr').length > 1) {
                _prev.find('td:last-child').html('<button type="button" class="btn btn-danger remove-row"><i class="icon-remove"></i></button>');
            }
            var num = 0;
            var _length = $('#plan-table tr:last-child input[type=text]').length;
            $('#plan-table tr:last-child input[type=text]').each(function (index) {
                if ($.trim($(this).val()) != '') {
                    num++;
                }
            });
            if (num == _length) {
                $('.add-row-btn').removeClass('sr-only');
            } else {
                $('.add-row-btn').addClass('sr-only');
            }
        });

        // 初始化时间选择插件
        function initDatetimepicker() {
            $('.plan_start_time').datetimepicker({
                language: 'zh-CN',
                format: 'yyyy-mm-dd',
                weekStart: 1,
                todayBtn: 1,
                autoclose: 1,
                todayHighlight: 1,
                /*startDate : '2014-3-21',
                endDate : new Date(),*/
                pickerPosition: 'top-right',
                startView: 2,
                minView: 2,
                forceParse: 0
            });
            $('.plan_stop_time').datetimepicker({
                language: 'zh-CN',
                format: 'yyyy-mm-dd',
                weekStart: 1,
                todayBtn: 1,
                autoclose: 1,
                todayHighlight: 1,
                /*startDate : new Date(),*/
                pickerPosition: 'top-right',
                startView: 2,
                minView: 2,
                forceParse: 0
            });
        }

        initDatetimepicker();

    })
});